package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.*;
import com.sky.service.OrderService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.OrderVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private OrdersDetailMapper ordersDetailMapper;

    /**
     * 用户端添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppCart(ShoppingCartDTO shoppingCartDTO) {
        // 1. 根据菜品id/套餐id查询数据库
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        ShoppingCart cart = shoppingCartMapper.getByCondition(shoppingCart);
        if (cart != null) {
            // 2. 查到了，认为商品已经在购物车，设置number为之前的数量+1，执行update操作
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        } else {
            // 3. 没有查到，获取商品数据，执行insert操作
            Long dishId = shoppingCartDTO.getDishId();
            cart = new ShoppingCart();
            // 设置创建时间
            cart.setCreateTime(LocalDateTime.now());
            // 设置用户id
            cart.setUserId(BaseContext.getCurrentId());
            // 设置数量为1
            cart.setNumber(1);
            if (dishId != null) {
                // 添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                cart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
                cart.setDishId(dishId);
            } else {
                // 添加的是套餐
                SetmealVO setmealVO = setmealMapper.getBySetmealId(shoppingCartDTO.getSetmealId());
                cart.setName(setmealVO.getName());
                cart.setImage(setmealVO.getImage());
                cart.setAmount(setmealVO.getPrice());
                cart.setSetmealId(shoppingCartDTO.getSetmealId());
            }
            shoppingCartMapper.insert(cart);

        }



    }

    @Override
    public List<ShoppingCart> getByUserId() {

        return shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 再来一单
     * @param id
     */
    @Override
    public void repititionOrders(Long id) {
       //1.获取用户id
        Long userId = BaseContext.getCurrentId();
        //2.根据订单id查询单前订单详情 集合不能直接转移因属性不一致
        List<OrderDetail> orderDetailList = ordersDetailMapper.getByOrderId(id);
        //3.将订单详情对象转换为购物车对象 这样需要遍历将数据一个一个转移
        for (OrderDetail orderDetail : orderDetailList) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail,shoppingCart);
            //将购物车没有的数据重新再set进去 调用新增购物车数据
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }



}
